package com.thinkfit.jy.view;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;
import com.thinkfit.jy.utils.FloatingManager;

public class FloatingView extends FrameLayout {
    private Context mContext;
    private View mView;
    private ImageView mImageView;
    ConstraintLayout clBg;
    private int mTouchStartX, mTouchStartY;//手指按下时坐标
    private WindowManager.LayoutParams mParams;
    private FloatingManager mWindowManager;
    int width,height;

    public FloatingView(Context context) {
        super(context);
        mContext = context.getApplicationContext();
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        width = dm.widthPixels;
        height= dm.heightPixels;
        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        mView = mLayoutInflater.inflate(R.layout.item_floating, null);
        clBg = mView.findViewById(R.id.cl_bg);
        mImageView = (ImageView) mView.findViewById(R.id.iv_item);
        mImageView.setOnTouchListener(mOnTouchListener);
        mWindowManager = FloatingManager.getInstance(mContext);
    }

    /**
     * 加载图片
     * @param res
     */
    public void setImageResource(int res){
        mImageView.setImageResource(res);
    }

    /**
     * 加载网络图片
     * @param url
     */
    public void setImageUrl(String url){
        BaseUtils.StaticParams.loadImage(mContext, 0, url, mImageView);
    }

    /**
     * 设置背景
     * @param res
     */
    public void setClBg(int res){
        clBg.setBackgroundResource(res);
    }


    public void show() {
        mParams = new WindowManager.LayoutParams();
        mParams.gravity = Gravity.TOP | Gravity.LEFT;
        mParams.x = 0;
        mParams.y = 100;
        //总是出现在应用程序窗口之上
        mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        //设置图片格式，效果为背景透明
//        mParams.format = PixelFormat.RGBA_8888;
        mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        mParams.width = LayoutParams.WRAP_CONTENT;
        mParams.height = LayoutParams.WRAP_CONTENT;
        mWindowManager.addView(mView, mParams);
        //逐帧动画
//        AnimationDrawable animationDrawable=(AnimationDrawable)mImageView.getDrawable();
//        animationDrawable.start();
    }

    public void hide() {
        mWindowManager.removeView(mView);
    }

    private OnTouchListener mOnTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mTouchStartX = (int) event.getRawX();
                    mTouchStartY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mTouchStartX = (int) event.getRawX();
                    mTouchStartY = (int) event.getRawY();
                    mParams.x = (int) event.getRawX();
                    mParams.y = (int) event.getRawY();//相对于屏幕左上角的位置
                    mWindowManager.updateView(mView, mParams);
                    break;
                case MotionEvent.ACTION_UP:
                    if(mTouchStartX < width/2){
                        mParams.x = 0;
                        mParams.y = mTouchStartY;
                        mWindowManager.updateView(mView, mParams);
                    }else {
                        mParams.x = width-mView.getWidth();
                        mParams.y = mTouchStartY;
                        mWindowManager.updateView(mView, mParams);
                    }
                    break;
            }
            return true;
        }
    };
}
